package com.fu.jdbc;

import com.fu.jdbc2.Myconnection3;
import com.fu.utils.JDBCUtils;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;

/**
 * @author 付大大
 * @description
 * @create 2021-01-31 11:03
 */
public class MyDataSource implements DataSource {

    private static List<Connection> pool = Collections.synchronizedList(new ArrayList<>());


    static {
        for (int i = 0; i < 10; i++) {
            Connection connection = JDBCUtils.getConnection();
            pool.add(connection);
        }
    }


//
//    @Override
//    public Connection getConnection() throws SQLException {
//        if (pool.size() > 0) {
//            Connection remove = pool.remove(0);
////            Myconnection2 myconnection2 = new Myconnection2(remove, pool);
//            Myconnection3 myconnection3 = new Myconnection3(remove, pool);
//            return myconnection3;
//        } else {
//            throw new RuntimeException("连接的数量已经用尽");
//        }
//    }


    @Override
    public Connection getConnection() throws SQLException {
        if (pool.size() > 0) {
            Connection remove = pool.remove(0);
            Connection proxyCon = (Connection) Proxy.newProxyInstance(remove.getClass().getClassLoader(), new Class[]{Connection.class}, new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    if (method.getName().equals("close")) {
                        pool.add(remove);
                        return null;
                    } else {
                        return method.invoke(remove, args);
                    }
                }
            });
            return proxyCon;
        } else {
            throw new RuntimeException("连接的数量已经用尽");
        }
    }

    public int getSize() {
        return pool.size();
    }


    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return null;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {

    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {

    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }
}
